#!/data/data/com.termux/files/usr/bin/bash
#访问手机内部储存

#检查依赖
if [ ! -e $PREFIX/bin/tar ]; then
        echo "y" | apt install tar
fi

if [ ! -e $PREFIX/bin/pv  ]; then
        echo "y" | apt install pv
fi

if [ ! -e $PREFIX/bin/aria2c ]; then
    apt update
        echo "y" | apt install aria2
fi

if [ ! -e $PREFIX/bin/proot ]; then
        echo "y" | apt install proot
fi
#判断架构
case $(uname -m) in
aarch64)
  archtype="arm64"
  ;;
arm64)
  archtype="arm64"
  ;;
armv8a)
  archtype="arm64"
  ;;
arm)
  archtype="armhf"
  ;;
armv7l)
  archtype="armhf"
  ;;
armhf)
  archtype="armhf"
  ;;
amd64)
  archtype="amd64"
  ;;
x86_64)
  archtype="amd64"
  ;;
s390x)
  archtype="s390x"
  ;;
ppc64el)
  archtype="ppc64el"
  ;;
*)
  echo "不支持的架构 $(uname -m) 镜像站的ubuntu-base可能没有对应你设备的包"
  exit 1
  ;;
esac
#下载镜像
[ -d ".ubuntu" ] || echo "检测到你没有安装ubuntu"

if [ -d "/data/data/com.termux/files/home/.ubuntu/ubuntu-fs" ]; then
  downloaded=1
  echo "检测到您已安装ubuntu"
fi
folder=ubuntu-fs
#创建文件夹
[ "$(pwd)" = "/data/data/com.termux/files/home" ] || cd /data/data/com.termux/files/home #确定是否在家目录
if [ ! -d ".ubuntu" ]; then
        mkdir .ubuntu
fi

if [ ! -d ".ubuntu/$folder" ]; then
        mkdir .ubuntu/ubuntu-fs
fi

if [ ! -d ".ubuntu/ubuntu-binds" ]; then
        mkdir .ubuntu/ubuntu-binds
fi

if [ ! -d ".ubuntu/image-backup" ]; then
        mkdir .ubuntu/image-backup
fi

cd ~
#if [ "$downloaded" != 1 ];then
if [ ! -f /data/data/com.termux/files/home/.ubuntu/image-backup/ubuntu-base-20.10-base.tar.gz ]; then
  echo "正在从清华大学开源镜像站下载容器镜像"
  aria2c -x 16 -k 1M --split 16 -o ubuntu-base-20.10-base.tar.gz "https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cdimage/ubuntu-base/releases/20.10/release/ubuntu-base-20.10-base-${archtype}.tar.gz"

fi


#解压
echo "正在解压镜像..."
if [ "$(uname -o)" = "Android" ]; then
  pv ubuntu-base-20.10-base.tar.gz | proot --link2symlink tar -pzxf - -C .ubuntu/$folder
else
  pv ubuntu-base-20.10-base.tar.gz | tar -pzxf - -C .ubuntu/$folder
fi

#备份镜像

mv ~/ubuntu-base-20.10-base.tar.gz ~/.ubuntu/image-backup

#更改ubuntu系统DNS服务器
DNS="nameserver 8.8.8.8\nnameserver 114.14.114.114"
echo -e "${DNS}" > ~/.ubuntu/ubuntu-fs/etc/resolv.conf
#更改下载源
mirrors="deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy main restricted\ndeb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy-updates main restricted\ndeb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy universe\ndeb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy-updates universe\ndeb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy multiverse\ndeb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy-updates multiverse\ndeb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy-backports main restricted universe multiverse\ndeb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy-security main restricted\ndeb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy-security universe\ndeb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ groovy-security multiverse"
echo -e "${mirrors}"  > ~/.ubuntu/ubuntu-fs/etc/apt/sources.list
#建立启动脚本
cat > $PREFIX/bin/startubuntu <<- EOM
#!/bin/bash
cd ~/.ubuntu
## unset LD_PRELOAD in case termux-exec is installed
unset LD_PRELOAD
command="proot"
command+=" --link2symlink"
command+=" -0"
command+=" -r $folder"
if [ -n "\$(ls -A ubuntu-binds)" ]; then
    for f in ubuntu-binds/* ;do
      . \$f
    done
fi
command+=" -b /dev"
command+=" -b /proc"
command+=" -b ubuntu-fs/root:/dev/shm"
## uncomment the following line to have access to the home directory of termux
#command+=" -b /data/data/com.termux/files/home:/root"
## uncomment the following line to mount /sdcard directly to /
#command+=" -b /sdcard"
command+=" -w /root"
command+=" /usr/bin/env -i"
command+=" HOME=/root"
command+=" PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/local/games"
command+=" TERM=\$TERM"
command+=" LANG=C.UTF-8"
command+=" /bin/bash --login"
com="\$@"
if [ -z "\$1" ];then
    exec \$command
else
    \$command -c "\$com"
fi
EOM

termux-fix-shebang $PREFIX/bin/startubuntu

chmod +x /data/data/com.termux/files/usr/bin/startubuntu

echo "现在你可以运行startubuntu指令来启动ubuntu系统了,软件源和DNS服务器已经更换"
echo "进去后记得运行apt update && apt upgrade来进行一波全面的更新"

exit
